/***************************************************************************
创建者：华磊
 开始时间：               : 2020.1１.09
 copyright            : (C) 深圳市华友高科有限公司
 修改说明：
 2016 华磊 对接数据显示,修改数据刷新的策略
 2016 华磊 对接 菜单按钮功能
 2017 华磊 顶部长信息乱码修复

 ***************************************************************************
 *   顶部视窗对话框
*                                                                         *
 ***************************************************************************/
#include "topview.h"
#include "ui_topview.h"
#include <QComboBox>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QSpacerItem>
#include <QDebug>
#include <qsettings.h>
#include "hmicommondefine.h"
#include "instructionEnum.h"



TopView::TopView(int styleId,QWidget *parent) :
    QWidget(parent),
    ui(new Ui::TopView)
{
    ui->setupUi(this);

    isLongMsg = false;
    isBlack = true;
    colorChangeCount = 0;


    setGeometry(rect().x(), rect().y(),600, 125);

    initialDisplayStyle(styleId);


    changeSpeedView = new ChangeSpeed;
    controlModeView = new ControlMode;
    currentCoordinateView = new CurrentCoordinate();//父窗口不能为top否则显示不下．
    debugModeView = new DebugMode;
    robotListView = new RobotList;
    jogCoordinateView = new JogCoordinate;

    //jogCoordinateView->setGeometry(300, 110,160,160);
    jogCoordinateView->setWindowFlags(Qt::FramelessWindowHint);
    jogCoordinateView->setStyleSheet("outline: none;");            //无虚线框
    //changeSpeedView->setGeometry(100,108,446,144);
    changeSpeedView->setWindowFlags(Qt::FramelessWindowHint);
    changeSpeedView->setStyleSheet("outline: none;");            //无虚线框
    //controlModeView->setGeometry(400,110,100,145);
    controlModeView->setWindowFlags(Qt::FramelessWindowHint);
    controlModeView->setStyleSheet("outline: none;");            //无虚线框
    //currentCoordinateView->setGeometry(400,110,245,145);
    currentCoordinateView->setWindowFlags(Qt::FramelessWindowHint);
    currentCoordinateView->setStyleSheet("outline: none;");            //无虚线框
    //debugModeView->setGeometry(220,110,175,100);
    debugModeView->setWindowFlags(Qt::FramelessWindowHint);
    debugModeView->setStyleSheet("outline: none;");            //无虚线框
    //robotListView->setGeometry(450,110,210,270);
    robotListView->setWindowFlags(Qt::FramelessWindowHint);
    robotListView->setStyleSheet("outline: none;");            //无虚线框
    setAttribute(Qt::WA_QuitOnClose, false);

    robotListView->topView=this;
    jogCoordinateView->topView=this;
    debugModeView->topView=this;
    currentCoordinateView->topView=this;
    controlModeView->topView=this;
    changeSpeedView->topView=this;

    //第一次默认需要全部更新
    tmpAutoSpeed = -1;
    tmpMaunalSpeed = -1;
    tmpDebugMode = -1;
//    tmpLine = "*";
    tmpJogCoordinate = -1;
    tmpControlMode = -1;

    tmpToolCoordinate = -1;
    tmpUserCoordinate = -1;

    tmpAccount = -1;
    tmpMotorStatus = -1;
    tmpFileName = "";

    tBlack.setColor(QPalette::ButtonText,QColor(75,75,75));

    tRed.setColor(QPalette::ButtonText,QColor(213,62,62));



}

TopView::~TopView()
{
    delete ui;
}

void TopView::updateCurrentRobotName(QString nameIn)
{
    ui->robot_btn->setText(nameIn);
}

void TopView::windowStatusSignal()
{
    emit showWindowStatus();
}

void TopView::on_maunal_btn_clicked()
{
    emit useFilmSignal();
    if(!changeSpeedView->isHidden())
    {
        changeSpeedView->hide();
    }
    else
    {
        hideChildView();
        changeSpeedView->show();
        changeSpeedView->setGeometry(100,60,446,144);

        //更新当前子窗口状态
        changeSpeedView->beforeAutoSpeed = -1;
        changeSpeedView->beforeMaunalSpeed = -1;
        //每次打开前都先赋值初值,这样进入小窗口后就能保证肯定刷新
        //存在情况:角色切换后,因为before值和当前值可能是一致的导致不刷新?
        changeSpeedView->updateAutoAdd();
        changeSpeedView->updateMaunalAdd();
    }
}

void TopView::on_step_btn_clicked()
{
    emit useFilmSignal();
    if(!debugModeView->isHidden())
    {
        debugModeView->hide();
    }
    else
    {
        hideChildView();
        debugModeView->show();
        debugModeView->setGeometry(220,60,121,106);
        //更新当前子窗口状态
        debugModeView->updateCurrentDebugData();
    }

}

void TopView::on_joint_btn_clicked()
{
    emit useFilmSignal();
    if(!jogCoordinateView->isHidden())
    {
        jogCoordinateView->hide();
    }
    else
    {
        hideChildView();
        jogCoordinateView->show();
        jogCoordinateView->setGeometry(300, 60 ,161,176);
        //更新当前子窗口状态
        jogCoordinateView->updateCurrentJogDate();
    }
}

void TopView::on_jog_btn_clicked()
{
    emit useFilmSignal();
    if(!controlModeView->isHidden())
    {
        controlModeView->hide();
    }
    else
    {
        hideChildView();
        controlModeView->show();
        controlModeView->setGeometry(350,60,101,180);
        //更新当前子窗口状态
        controlModeView->updateCurrentModelData();
    }

}

void TopView::on_coodinate_btn_clicked()
{
    emit useFilmSignal();
    if(!currentCoordinateView->isHidden())
    {
        currentCoordinateView->hide();
    }
    else
    {
        hideChildView();
        currentCoordinateView->show();
        currentCoordinateView->setGeometry(330,60,256,156);
        //更新当前子窗口状态
        currentCoordinateView->updateCoordinateData();
    }


}

void TopView::on_robot_btn_clicked()
{
    emit updateRobotListView_signal();
    emit useFilmSignal();
    if(!robotListView->isHidden())
    {
        robotListView->hide();
    }
    else
    {
        hideChildView();
        robotListView->show();
        robotListView->setGeometry(310,60,280,270);
        //更新当前子窗口状态
        robotListView->updateRobotView();
    }
}

void TopView::on_homemenu_btn_clicked()
{
    emit homeMenu();
}

void TopView::updateChangeSpeedView()
{
    if(tmpAutoSpeed != autoSpeed || tmpMaunalSpeed != maunalSpeed)
    {
        QString number = "";
        QString number2 = "";

        if(autoSpeed<2)
        {
            number=QString::number(autoSpeed,'f',1);
        }
        else
        {
            number=QString::number(autoSpeed);
        }

        if(maunalSpeed<2)
        {
            number2=QString::number(maunalSpeed,'f',1);
        }
        else
        {
            number2=QString::number(maunalSpeed);
        }


        ui->maunal_btn->setText(tr("自动")+number+"\n"+tr("手动")+number2);
        tmpAutoSpeed = autoSpeed;
        tmpMaunalSpeed = maunalSpeed;
    }
}

void TopView::updateCurrentCoordinateView()
{
    if(tmpToolCoordinate != currentToolCoordinate || tmpUserCoordinate != currentUserCoordinate)
    {
        ui->coodinate_btn->setText(tr("用户:")+QString::number(currentUserCoordinate)+"\n"+
                                   tr("工具:")+QString::number(currentToolCoordinate));
    }
}

void TopView::updateDebugModeView()
{
    //qDebug()<<debugMode;
    if(tmpDebugMode != debugMode)
    {
        switch(debugMode)
        {
            case EM_DEBUG_MODE_STEP:
                if(!ui->step_btn->isEnabled())
                {
                    ui->step_btn->setEnabled(true);
                }
                ui->step_btn->setText(tr("单步"));
                break;
            case EM_DEBUG_MODE_CONTINUE:
                if(!ui->step_btn->isEnabled())
                {
                    ui->step_btn->setEnabled(true);
                }
                ui->step_btn->setText(tr("连续"));

                break;
            default:
                if(ui->step_btn->isEnabled())
                {
                    ui->step_btn->setEnabled(false);
                }
                break;
        }
        tmpDebugMode = debugMode;
    }
}

//void TopView::updateRobotListView()
//{
////    if(line != tmpLine)
////    {
////        ui->robot_btn->setText(tr("机器人:\n")+line);
////        tmpLine = line;
////    }
//    emit updateRobotListView_signal();
//}

void TopView::updateControlModeView()
{
    //qDebug()<<controlMode;
    if(tmpControlMode != controlMode)
    {
        switch(controlMode)
        {
            case EM_CONTROL_MODE_T1:
                ui->jog_btn->setText(tr("手动T1"));
                break;
            case EM_CONTROL_MODE_T2:
                ui->jog_btn->setText(tr("手动T2"));
                break;
            case EM_CONTROL_MODE_AUTO:
                ui->jog_btn->setText(tr("自动"));
                break;
            case EM_CONTROL_MODE_EXT:
                ui->jog_btn->setText(tr("外部"));
                break;
            case EM_CONTROL_MODE_FLEET:
                ui->jog_btn->setText(tr("调度"));
                break;
        default:
                break;
        }
        tmpControlMode = controlMode;
    }
}

void TopView::updateJogCoordinateView()
{
    if(jogCoordinate != tmpJogCoordinate)
    {
        switch(jogCoordinate)
        {
            case EM_JOG_COORDINATE_JOINT:
                ui->joint_btn->setText(tr("关节\n坐标系"));
                break;
            case EM_JOG_COORDINATE_TOOL:
                ui->joint_btn->setText(tr("工具\n坐标系"));
                break;
            case EM_JOG_COORDINATE_USER:
                ui->joint_btn->setText(tr("用户\n坐标系"));
                break;
            case EM_JOG_COORDINATE_WORLD:
                ui->joint_btn->setText(tr("世界\n坐标系"));
                break;
            default:
                break;
        }
        tmpJogCoordinate = jogCoordinate;
    }
}

void TopView::hideChildView()//2020.12.05 当窗口隐藏时候,也会刷新所有数据
{
    changeSpeedView->hide();
    controlModeView->hide();
    currentCoordinateView->hide();
    debugModeView->hide();
    robotListView->hide();
    jogCoordinateView->hide();
    updateAllChildView();
}

void TopView::updateAllChildView()//表面小窗口 全部刷新 定时
{
    updateChangeSpeedView();
    updateDebugModeView();
    updateCurrentCoordinateView();
//    updateRobotListView();
    updateControlModeView();
    updateJogCoordinateView();

    //蓝色部分
    updateShowWindow();
}

void TopView::hideAllTopViewSlot()
{
    changeSpeedView->hide();
    controlModeView->hide();
    currentCoordinateView->hide();
    debugModeView->hide();
    robotListView->hide();
    jogCoordinateView->hide();
}


void TopView::on_file_btn_clicked()
{
    emit showFileName();
}

void TopView::on_account_btn_clicked()
{
    emit changeAccount();
}

void TopView::updateShowWindow()
{

    ui->menu_btn->setText(shortMessage);

    if(isLongMsg)
    {
        colorChangeCount++;
        if(colorChangeCount == 30)
        {
            ui->welcome_btn->setPalette(tRed);
            //ui->welcome_btn->setStyleSheet("font-color:red");
            isBlack = false;
        }
        else if(colorChangeCount == 60)
        {
            ui->welcome_btn->setPalette(tBlack);
            //ui->welcome_btn->setStyleSheet("font-color:black");
            colorChangeCount = 0;
            isBlack = true;
        }
        ui->welcome_btn->setText(tr(longMessage.toLocal8Bit()));//2017.02.06 改:乱码
    }
    else
    {
        colorChangeCount = 0;
        if(!isBlack)
        {
            ui->welcome_btn->setPalette(tBlack);
            //ui->welcome_btn->setStyleSheet("font-color:black");
            isBlack = true;
        }
        ui->welcome_btn->setText(longMessage);//2017.02.06 改:乱码
    }


    if(tmpFileName != fileName)
    {
        if(fileName == "")
        {
            fileName = "-";
        }
        ui->file_btn->setText(fileName);
        tmpFileName = fileName;
    }

    if(tmpMotorStatus != motorStatus)
    {
        switch(motorStatus)
        {
            case EM_MOTOR_STATUS_SERVO_ERROR:
                ui->motor_btn->setText(tr("伺服异常"));
                ui->motor_btn->setStyleSheet("border-width: 0px;  border-style: solid;background-color: rgb(255,69,0);margin-top: 0ex;");
                break;
            case EM_MOTOR_STATUS_SERVO_ON:
                emit homeViewMotorSignal(EM_MOTOR_STATUS_SERVO_ON);
                ui->motor_btn->setText(tr("伺服开启"));
                ui->motor_btn->setStyleSheet("border-width: 0px;  border-style: solid;background-color: rgb(162,205,50);margin-top: 0ex;");
                break;
            case EM_MOTOR_STATUS_SERVO_OFF:
                emit homeViewMotorSignal(EM_MOTOR_STATUS_SERVO_OFF);
                ui->motor_btn->setText(tr("伺服关闭"));
                ui->motor_btn->setStyleSheet("border-width: 0px;  border-style: solid;background-color: rgb(212,212,212);margin-top: 0ex;");
                break;
            default:
                ui->motor_btn->setText(tr("伺服关闭"));
                ui->motor_btn->setStyleSheet("border-width: 0px;  border-style: solid;background-color: rgb(212,212,212);margin-top: 0ex;");
                break;
        }
        tmpMotorStatus = motorStatus;
    }

    if(tmpAccount != account)
    {
        switch(account)
        {
            case EM_ACCOUNT_ADMIN:
                ui->account_btn->setStyleSheet(QString("border-image: url(%1);").arg("/agvwork/AgvHmi/baseBag/hmiProject/resources/topviewresources/m_power_manager"));
                break;
            case EM_ACCOUNT_USER:
                ui->account_btn->setStyleSheet(QString("border-image: url(%1);").arg("/agvwork/AgvHmi/baseBag/hmiProject/resources/topviewresources/m_power_guest"));
                break;
            default:
                break;
        }
        tmpAccount = account;
    }
}

void TopView::changeLanguage(int language)
{
    if(1 == language)
    {
        translator.load("/agvwork/AgvHmi/baseBag/hmiProject/language/topViewEnglishLan.qm");
        qApp->installTranslator(&translator);
    }
    else
    {
        qApp->removeTranslator(&translator);
    }
    ui->retranslateUi(this);

    changeSpeedView->changeLanguage();
    controlModeView->changeLanguage();
    currentCoordinateView->changeLanguage();
    debugModeView->changeLanguage();
    jogCoordinateView->changeLanguage();
    robotListView->changeLanguage();

    //修复数据:存在问题 手动选择等数据显示不正确
    //恢复默认值,表示需要全部重新更新
    tmpAutoSpeed = -1;
    tmpMaunalSpeed = -1;
    tmpDebugMode = -1;
//    tmpLine = "*";
    tmpJogCoordinate = -1;
    tmpControlMode = -1;

    tmpAccount = -1;
    tmpMotorStatus = -1;
    tmpFileName = "";

}



void TopView::initialDisplayStyle(int styleId)
{
    QSettings settings(D_SHOW_STYLE_PATH, QSettings::IniFormat);

    int  agvProductFlag= settings.value("ProductCharacter/agvProduct").toInt();
    if(1!=agvProductFlag)
    {
        ui->maunal_btn_map->hide();
    }

    switch (styleId) {
    case 1:
        //ui->设置所有控件的位置和大小。
        this->setGeometry(0,0,800,100);
        ui->splitter->setGeometry(10,0,790,40);
        ui->welcome_btn->setGeometry(0,45,800,20);
        ui->motor_btn->setGeometry(0,70,135,20);
        ui->menu_btn->setGeometry(140,70,330,20);
        ui->file_btn->setGeometry(475,70,325,20);

        break;
    default:
        break;
    }
}


void TopView::on_maunal_btn_map_clicked()
{
    emit showMapView();
    return;
}
